package com.zhuangjie.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaungjie.base.conduit.TypeR;
import com.zhuangjie.security.provider.UserProvider;
import com.zhuangjie.shop.vo.ShopInfoVO;
import com.zhuangjie.user.entity.AddressBook;
import com.zhuangjie.user.feign.ShopFeignService;
import com.zhuangjie.user.mapper.AddressBookMapper;
import com.zhuangjie.user.service.AddressBookService;
import com.zhuangjie.user.vo.AddressBookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收货地址表 服务实现类
 * </p>
 *
 * @author zhuangjie
 * @since 2023-10-15
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Autowired
    private ShopFeignService shopFeignService;

    @Override
    public void saveAddress(AddressBook addressBook) {
        if (addressBook.getId() == null) {
            // 新增设置userId
            addressBook.setUserId(UserProvider.currentUserId());
        }
        // 根据id自动新增或更新
        this.saveOrUpdate(addressBook);
    }

    @Override
    public List<AddressBookVO> preferredAddressList(Long shopId) throws Exception {
        // 查出店铺信息
        ShopInfoVO shopInfoVO = TypeR.extract(shopFeignService.getShopInfoById(shopId));
        // 从店铺中找出合理的收货地址信息
        Long userId = UserProvider.currentUserId();
        List<AddressBookVO> addressBookVOList = this.baseMapper.preferredAddressList(userId, shopInfoVO.getLongitude(),shopInfoVO.getLatitude(), shopInfoVO.getBusinessScope());
        // 尝试填充shopId
//        if (addressBookVOList != null && ! addressBookVOList.isEmpty()) {
//            addressBookVOList.forEach(addressBookVO -> addressBookVO.setShopId(shopId));
//        }
        return addressBookVOList;
    }

    @Override
    public void removeUserAddressBookById(Long addressBookId) {
        LambdaQueryWrapper<AddressBook> removeUserAddressBookWrapper = new LambdaQueryWrapper<AddressBook>()
                .eq(AddressBook::getUserId, UserProvider.currentUserId())
                .eq(AddressBook::getId, addressBookId);
        int delete = this.baseMapper.delete(removeUserAddressBookWrapper);
        if (delete < 1 ) {
            throw new RuntimeException("删除地址项失败！");
        }
    }

    @Override
    public AddressBook preferredAddress(Long shopId) throws Exception {
        // 查出店铺信息
        ShopInfoVO shopInfoVO = TypeR.extract(shopFeignService.getShopInfoById(shopId));
        // 从店铺中找出合理的收货地址信息
        Long userId = UserProvider.currentUserId();
        AddressBookVO addressBookVO = this.baseMapper.preferredAddress(userId, shopInfoVO.getLongitude(),shopInfoVO.getLatitude(), shopInfoVO.getBusinessScope());
        // 尝试填充shopId
        // if (addressBookVO != null ) addressBookVO.setShopId(shopId);
        return addressBookVO;
    }

    @Override
    public List<AddressBook> getUserAddressList() {
        Long userId = UserProvider.currentUserId();
        LambdaQueryWrapper<AddressBook> queryUserAddressListWrapper = Wrappers.lambdaQuery(AddressBook.class)
                .eq(AddressBook::getUserId, userId)
                .orderByDesc(AddressBook::getUpdateTime);
        return this.list(queryUserAddressListWrapper);
    }

    @Override
    public AddressBook getUserAddressBookById(Long addressBookId) {
        LambdaQueryWrapper<AddressBook> queryUserAddressBookWrapper = Wrappers.lambdaQuery(AddressBook.class)
                .eq(AddressBook::getUserId, UserProvider.currentUserId())
                .eq(AddressBook::getId, addressBookId);
        AddressBook addressBook = this.baseMapper.selectOne(queryUserAddressBookWrapper);
        return addressBook;
    }

    @Override
    public AddressBook preferredAddress(Double longitude, Double latitude,Double BusinessScope) {
        Long userId = UserProvider.currentUserId();
        return  this.baseMapper.preferredAddress(userId,longitude,latitude, BusinessScope);
    }
}
